<?php
class ModelSession extends  BaseModel{
	var $ssid =null;
	var $reqs_id = null;
	var $reqs_name = null;
	var $resp_id = null;
	var $resp_name = null;
	var $last_time = null;

	function __construct(){
		parent::__construct();
	}


	public static function generateByResponse($resquest,$responser_uuid,$responser_nick){
		$ms = new ModelSession();
		$ms->reqs_id = $resquest->requester;
		$ms->reqs_name = $resquest->nickName;
		$ms->resp_id = $responser_uuid;
		$ms->resp_name=$responser_nick;
		$ms->last_time = date("Y-m-d H:i:s");

		return $ms;
	}

	public static function findByAssociate($uuid_1,$uuid_2){
		$sql = "select ssid,reqs,reqs_name,resp,resp_name,last_activity from ps_session where (reqs = '$uuid_1' and resp = '$uuid_2') or (reqs = '$uuid_2' and resp = '$uuid_1')";
		return self::findModelSessionBySql($sql);
	}
	public static function findAssociateUUID($uuid){
		$ms = findSessionByUUID($uuid);
		return $ms->reqs_id == $uuid?$ms->resp_id:$ms->reqs_id;
	}
	public static function findSessionByUUID($uuid){
		$sql = "select ssid,reqs,reqs_name,resp,resp_name,last_activity from ps_session where reqs = '$uuid' or resp = '$uuid'";
		return self::findModelSessionBySql($sql);
	}

	public function insertIntoDB(){
		$sql = "insert into ps_session (reqs,reqs_name,resp,resp_name,last_activity)
		values ('$this->reqs_id','$this->reqs_name','$this->resp_id','$this->resp_name','$this->last_time')";
		$db = parent::getMyDB();
		$this->ssid = $db->insertOneRecord($sql);
		return isset($this->ssid);
	}
	
	public function deleteFromDB(){
		$sql = "delete from ps_session where ssid = '$this->ssid'";
		$ret = parent::getMyDB()->exec($sql);
		return $ret;
	}

	public static function findBySSID($ssid){
		$sql = "select ssid,reqs,reqs_name,resp,resp_name,last_activity from ps_session where ssid = $ssid";

		return self::findModelSessionBySql($sql);
	}

	private static function findModelSessionBySql($sql){
		$db = MyDB::getInstance();
		$ret = $db->queryOneRow($sql);
		if(!$ret) return null;

		$ms = new ModelSession();
		$ms->ssid = $ret['ssid'];
		$ms->reqs_id = $ret['reqs'];
		$ms->reqs_name = $ret['reqs_name'];
		$ms->resp_id = $ret['resp'];
		$ms->resp_name = $ret['resp_name'];
		$ms->last_time = $ret['last_activity'];

		return $ms;
	}

	public function updateLastActivity(){
		if(!isset($this->ssid)){
			return false;
		}else{
			$current = date("Y-m-d H:i:s");
			$sql = "update ps_session set last_activity = '$current' where ssid = '$this->ssid'";
			parent::getMyDB()->exec($sql);
			return true;
		}
	}

	public static function createTable(){
		$db = MyDB::getInstance();
		$sql_create = 'CREATE TABLE IF NOT EXISTS `ps_session` (
		`ssid` int(8) NOT NULL AUTO_INCREMENT,
		`reqs` char(255) NOT NULL,
		`reqs_name` varchar(255) NOT NULL,
		`resp` char(255) NOT NULL,
		`resp_name` varchar(255) NOT NULL,
		`last_activity` datetime NOT NULL DEFAULT "0000-00-00 00:00:00",
		PRIMARY KEY (`ssid`),
		FOREIGN KEY (`reqs`) REFERENCES `ps_user` (`uuid`),
		FOREIGN KEY (`resp`) REFERENCES `ps_user` (`uuid`)
		) ENGINE=InnoDB DEFAULT CHARSET=latin1';
		$db->exec($sql_create);
	}

	public static  function dropTable(){
		parent::dropTableByName('ps_session');
	}
}